package dS.ProjectGPS.GPS;

import dS.ProjectGPS.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.MessageConnection;
import javax.microedition.io.Connector;
import javax.wireless.messaging.TextMessage;

public class FormSMSSend extends Form {

  StringItem note = new StringItem("",
                                   "NOTE: SMS limits 160 characters per message. If over this would be sent as multiple messages");

  TextField number = new TextField("To:", Options.phNumber, 20,
                                   TextField.NUMERIC);

  String location = "\nLat: " + Navigator.formatLat(NMEA.lat) + "\n" + "Lon: " +
                    Navigator.formatLon(NMEA.lon) + "\n" + "Alt: " + NMEA.alt +
                    NMEA.altUnits + "\n" + "Speed: " + NMEA.speedKmh + "km/h\n" +
                    "Time: " + Navigator.formatTime(NMEA.UTC) + " UTC\n" +
                    "Date: " + Navigator.formatDate(NMEA.date);

  StringItem message = new StringItem("Data:", location);
  TextField extra = new TextField("Note:", "", 50, TextField.ANY);

  Ticker ticker1 = new Ticker("Send your location details to..");

  public FormSMSSend() {
    super("Send SMS");
    try {
      NDSInit();
    }
    catch (Exception ex) {
    }
  }

  public void NDSInit() throws Exception {
    append(note);
    append(number);
    append(extra);
    append(message);
    setTicker(ticker1);
  }

  public void sendSMS() {
    String recipient = number.getString();
    Options.phNumber = recipient;
    String address = "sms://" + recipient;

    MessageConnection smsconn = null;
    try {
      //Open the message connection.
      smsconn = (MessageConnection) Connector.open(address);
      TextMessage txtMessage = (TextMessage) smsconn.newMessage(
         MessageConnection.TEXT_MESSAGE);
      txtMessage.setPayloadText(extra.getString() + location);
      smsconn.send(txtMessage);
      smsconn.close();
    }
    catch (Exception e) {
    }
  }
}
